package com.vcaresolution.logistic.service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vcaresolution.logistic.form.PortForm;
import com.vcaresolution.logistic.pojo.City;
import com.vcaresolution.logistic.pojo.Country;
import com.vcaresolution.logistic.pojo.Port;
import com.vcaresolution.logistic.pojo.Sector;
import com.vcaresolution.logistic.pojo.State;
import com.vcaresolution.logistic.to.PortTO;
import com.vcaresolution.util.Utils;

@Service
public class PortServiceImpl extends BaseService implements PortService{

	private Port populatePortPojo(PortForm portForm){
		Port port = new Port();
		BeanUtils.copyProperties(portForm, port);
		
		return port;
	}
	
	@Override
	@Transactional
	public void add(PortForm portForm) {
		Port port = populatePortPojo(portForm);
		
		port.setSector(getDaoFactory().getUtilityDAO().findById(Sector.class, portForm.getSectorid()));
		port.setCountry(getDaoFactory().getUtilityDAO().findById(Country.class, portForm.getCountryid()));
		port.setState(getDaoFactory().getUtilityDAO().findById(State.class, portForm.getStateid()));
		port.setCity(getDaoFactory().getUtilityDAO().findById(City.class, portForm.getCityid()));
		port.setAuditdate(Utils.getCurrentDate());

		getDaoFactory().getUtilityDAO().add(port);
	}

	@Override
	@Transactional
	public void update(PortForm portForm) {
		Port port = populatePortPojo(portForm);
		
		port.setSector(getDaoFactory().getUtilityDAO().findById(Sector.class, portForm.getSectorid()));
		port.setCountry(getDaoFactory().getUtilityDAO().findById(Country.class, portForm.getCountryid()));
		port.setState(getDaoFactory().getUtilityDAO().findById(State.class, portForm.getStateid()));
		port.setCity(getDaoFactory().getUtilityDAO().findById(City.class, portForm.getCityid()));
		port.setAuditdate(Utils.getCurrentDate());

		getDaoFactory().getUtilityDAO().update(port);
	}

	@Override
	@Transactional
	public void delete(PortForm portForm) {
		getDaoFactory().getUtilityDAO().delete(populatePortPojo(portForm));
	}

	@Override
	@Transactional
	public PortTO findById(Serializable portID) {
		Port port = (Port) getDaoFactory().getUtilityDAO().findById(Port.class,portID);
		
		PortTO portTO = new PortTO();
		BeanUtils.copyProperties(port, portTO);
		
		portTO.setCityid(port.getCity().getCityid());
		portTO.setCityname(port.getCity().getCityname());
		
		portTO.setStateid(port.getState().getStateid());
		portTO.setStatename(port.getState().getStatename());
		
		portTO.setCountryid(port.getCountry().getCountryid());
		portTO.setCountryname(port.getCountry().getCountryname());
		
		portTO.setSectorid(port.getSector().getSectorid());
		portTO.setSectorname(port.getSector().getSectorname());
		
		return portTO; 
	}

	@Override
	@Transactional
	public List<PortTO> findByAll() {
		List<Port> portList = getDaoFactory().getUtilityDAO().findByAll(Port.class);
		
		List<PortTO> portFormList = new ArrayList<PortTO>();
		
		if( portList != null && !portList.isEmpty() ){
			PortTO portTO = null;
			for(Port port: portList){
				portTO = new PortTO();
				
				BeanUtils.copyProperties(port, portTO);
				
				portTO.setCityname(port.getCity().getCityname());
				portTO.setStatename(port.getState().getStatename());
				portTO.setCountryname(port.getCountry().getCountryname());
				portTO.setSectorname(port.getSector().getSectorname());
				
				portFormList.add(portTO);
			}
		}	
		return portFormList;
	}
}